<?php
/**************************************************************************************************
|
| AudioShare - Audio Sharing Script
| http://www.audiosharescript.com
| webmaster@audiosharescript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
|
| Copyright (c) 2010 AudioShareScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
STemplate::assign('issignuppage',"1");

$redirect = base64_decode($_REQUEST['redirect']);
$redirect = escape($redirect);

if ($_SESSION[USERID] != "" && $redirect != "")
{
	header("Location:$redirect");exit;
}

if($_REQUEST[register]!="")
{
	$username = cleanit($_REQUEST[username]);
	$username = str_replace("(", "", $username);
	$username = str_replace(")", "", $username);
	$username = str_replace(".", "", $username);
	$username = str_replace(" ", "", $username);
	$password = stripslashes($_REQUEST[password]);
	$confirmpassword = stripslashes($_REQUEST[confirmpassword]);
	$imagecode = stripslashes($_REQUEST[imagecode]);
	$email = stripslashes($_REQUEST[email]);
	$gender = stripslashes($_REQUEST[gender]);
	
	if ($username == "")
	{
		$error="$lang[571]";
	}
	elseif(!ereg("^[-_a-zA-Z0-9]*$",$username) )
	{
		$error="$lang[68]";
	}
	elseif (!verify_email_username($username))
	{
		$error = "$langev[6]";
	}
	elseif(!verify_valid_email($email))
	{
		$error = "$lang[498]";
	}
	elseif (!verify_email_unique($email))
	{
		$error = "$lang[500]";
	}
	elseif ($gender == "" || !is_numeric($gender))
	{
		$error="$lang[572]";
	}
	elseif ($password == "")
	{
		$error="$lang[573]";
	}
	elseif ($confirmpassword == "")
	{
		$error="$lang[574]";
	}
	elseif ($password != $confirmpassword)
	{
		$error="$lang[575]";
	}
	elseif($imagecode != $_SESSION[imagecode])
	{
		if ($config['enable_captcha'] != "0")
		{
			$error="$lang[576]";
		}
	}
	
	if ($error == "")
	{
		$md5pass = md5($password);
		if($config['man_mem_val'] == "1")
		{
			$addtosql = ", status='0'";
		}
		$query="INSERT INTO members SET email='".mysql_real_escape_string($email)."',username='".mysql_real_escape_string($username)."',gender='".mysql_real_escape_string($gender)."', password='".mysql_real_escape_string($md5pass)."', addtime='".time()."', lastlogin='".time()."' $addtosql";
		$result=$conn->execute($query);
		$userid = mysql_insert_id();
		
		if($userid != "" && is_numeric($userid) && $userid > 0)
		{
			$query="SELECT USERID,email,username,verified from members WHERE USERID='".mysql_real_escape_string($userid)."'";
			$result=$conn->execute($query);
			
			$SUSERID = $result->fields['USERID'];
			$SEMAIL = $result->fields['email'];
			$SUSERNAME = $result->fields['username'];
			$SVERIFIED = $result->fields['verified'];
			if($config['man_mem_val'] != "1")
			{
				$_SESSION[USERID]=$SUSERID;
				$_SESSION[EMAIL]=$SEMAIL;
				$_SESSION[USERNAME]=$SUSERNAME;
				$_SESSION[VERIFIED]=$SVERIFIED;
			}
			
			// Generate Verify Code Begin
			$verifycode = generateCode(5).time();
			$query = "INSERT INTO members_verifycode SET USERID='".mysql_real_escape_string($SUSERID)."', code='$verifycode'";
            $conn->execute($query);
			if(mysql_affected_rows()>=1)
			{
				$proceedtoemail = true;
			}
			else
			{
				$proceedtoemail = false;
			}
			// Generate Verify Code End
			
			// Send Welcome E-Mail Begin
			if ($proceedtoemail)
			{
                STemplate::assign('verifycode',$verifycode);
                $sendto = $SEMAIL;
                $sendername = $config['site_name'];
				STemplate::assign('sendername',$sendername);
                $from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='welcomeemail'";
                $executequery = $conn->execute($query);
                $subject = $executequery->fields['subject']." ".$sendername;
                $sendmailtemplate = $executequery->fields['template'];
                $sendmailbody=STemplate::fetch($sendmailtemplate);
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
			}
			// Send Welcome E-Mail End
			if($config['man_mem_val'] != "1")
			{
				if($redirect == "")
				{
					header("Location:$config[baseurl]");exit;
				}
				else
				{
					header("Location:$redirect");exit;
				}
			}
			else
			{
				$showmessage = "$langev[5]";
			}
		}
		else
		{
			$error="$lang[577]";
		}
	}
}

$pagetitle = "$lang[240]";
STemplate::assign('pagetitle',$pagetitle);

STemplate::assign('message',$message);
STemplate::assign('error',$error);

//TEMPLATES BEGIN
STemplate::display('header.tpl');
STemplate::display('signup.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>